package com.witmed.demo02

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
//import org.junit.Test

class Demo_flow {

//    @Test
    fun test01(){

        var myflow = flow {
            for (i in 1..3) {
                delay(1000)
                emit(i)
                println("emit send $i Thread=${Thread.currentThread().name}")
            }
        }
            .flowOn(Dispatchers.IO)//上游的切换线程

        runBlocking {
            myflow.buffer(20).onEach { value ->
                println("value=$value Thread=${Thread.currentThread().name}")
            }
            .launchIn(CoroutineScope(Dispatchers.Default)) //返回Job对象，指定下游所在的线程
            .join()
        }
    }
}